<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>考试管理</el-breadcrumb-item>
      <el-breadcrumb-item>考试列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="5">
          <el-select v-model="queryExamine.subject" clearable placeholder="请选择考试科目">
            <el-option
              v-for="item in subjectOptions"
              :key="item"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="5">
          <el-select v-model="queryExamine.stage" clearable placeholder="请选择考试阶段">
            <el-option
              v-for="item in stageOptions"
              :key="item"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-date-picker v-model="queryExamine.date" type="date" placeholder="请选择考试日期"
                          value-format="yyyy-MM-dd HH:mm:ss">
          </el-date-picker>
        </el-col>
        <el-col :span="3">
          <el-button icon="el-icon-search" @click="getExamineList">搜索</el-button>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" @click="addDialogVisible = true">新增考试</el-button>
        </el-col>
      </el-row>
    </el-card>

    <el-card>
      <!-- 考试列表区域 -->
      <el-table :data="examineList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="考试id" prop="id" v-if="false"></el-table-column>
        <el-table-column label="考试场次" prop="name" width="164px" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="考试科目" prop="subject" width="100px"></el-table-column>
        <el-table-column label="总人数" prop="examNumber" width="90px">
          <template slot-scope="scope">
            {{ scope.row.examNumber }}人
          </template>
        </el-table-column>
        <el-table-column label="报名人数" prop="appointNumber" width="100px">
          <template slot-scope="scope">
            {{ scope.row.appointNumber }}人
          </template>
        </el-table-column>
        <el-table-column label="通过人数" prop="passNumber" width="100px">
          <template slot-scope="scope">
            {{ scope.row.passNumber }}人
          </template>
        </el-table-column>
        <el-table-column label="考试阶段" prop="stage" width="100px">
          <template slot-scope="scope">
            <el-tag :type="scope.row.stage==='报名结束'?'warning':scope.row.stage==='考试结束'?'info':''">
              {{ scope.row.stage }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="考试日期" prop="date" width="100px"></el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <!-- 结束报名按钮 -->
            <el-tooltip effect="dark" content="结束报名" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-finished" size="mini" :disabled="scope.row.stage !== '报名中'"
                         @click="finishAppoint(scope.row.id)"></el-button>
            </el-tooltip>
            <!-- 分配教练按钮 -->
            <el-tooltip effect="dark" content="安排教练" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-setting" size="mini"
                         :disabled="(scope.row.stage !== '报名中' && scope.row.stage !== '报名结束')"
                         @click="setCoachDialog(scope.row.id)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="queryExamine.page" :page-sizes="[5, 10, 20, 50]" :page-size="queryExamine.size"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

    <!-- 新增考试的对话框 -->
    <el-dialog title="新增考试" :visible.sync="addDialogVisible" width="50%">
      <!-- 内容主体区域 -->
      <el-form :model="addExamineForm" :rules="addExamineFormRules" ref="addExamineFormRef" label-width="80px">
        <el-form-item label="考试场次" prop="name">
          <el-input v-model="addExamineForm.name" placeholder="请输入考试场次"></el-input>
        </el-form-item>
        <el-form-item label="考试科目" prop="subject">
          <el-radio v-model="addExamineForm.subject" v-for="item in subjectOptions" :key="item"
                    :label="item">{{ item }}
          </el-radio>
        </el-form-item>
        <el-form-item label="报名人数" prop="examNumber">
          <template>
            <el-input-number v-model.number="addExamineForm.examNumber" @change="examNumberHandleChange"
                             :min="1" :max="10000" label="请设置报名人数上线">
            </el-input-number>
          </template>
        </el-form-item>
        <el-form-item label="考试日期" prop="date">
          <el-date-picker v-model="addExamineForm.date" type="date" placeholder="请选择考试日期"
                          value-format="yyyy-MM-dd HH:mm:ss" :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogClosed">取 消</el-button>
        <el-button type="primary" @click="addExamine">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 安排教练的对话框 -->
    <el-dialog title="安排教练" :visible.sync="setCoachDialogVisible" width="60%" >

      <template>
        <el-transfer
          filterable
          filter-placeholder="请输入教练名字"
          v-model="examineCoach"
          :data="allCoach"
          :titles="['所有教练', '已分配教练']"
          :button-texts="['移除', '分配']">
        </el-transfer>
      </template>

      <span slot="footer" class="dialog-footer">
        <el-button @click="setCoachDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="setCoach">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "ExamineManage",
  data() {
    return {
      // 查询考试信息
      queryExamine: {
        page: 1,
        size: 10,
        subject: '',
        stage: '',
        date: ''
      },
      subjectOptions: ['科目一', '科目二', '科目三', '科目四'],
      stageOptions: ['报名中', '报名结束', '考试中', '考试结束'],
      // 考试信息列表
      examineList: [],
      total: 0,
      // 控制新增考试对话框的显示与隐藏
      addDialogVisible: false,
      // 新增考试
      addExamineForm: {
        name: '',
        subject: '',
        examNumber: 0,
        date: ''
      },
      addExamineFormRules: {
        name: [
          {required: true, message: '请输入考试场次', trigger: 'blur'},
          {
            min: 3,
            max: 32,
            message: '考试场次的长度在3~32个字符之间',
            trigger: 'blur'
          }
        ],
        subject: [
          {required: true, message: '请选择考试科目', trigger: 'blur'},
        ],
        examNumber: [
          {type: 'number', required: true, message: '请设置报名人数上线', trigger: 'blur'},
          {
            type: 'number',
            min: 1,
            max: 10000,
            message: '报名人数上线在1~10000之间',
            trigger: 'blur'
          }
        ],
        date: [
          {required: true, message: '请选择考试日期', trigger: 'change'},
        ]
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now()
        }
      },
      // 控制安排教练对话框的显示与隐藏
      setCoachDialogVisible: false,
      // 考试教练列表
      examineCoach: [],
      // 所有教练列表
      allCoach: [],
      examineId: 0
    }
  },

  created() {
    this.getExamineList()
  },
  methods: {
    // 获取考试列表
    getExamineList() {
      // console.log(this.queryExamine);
      this.$http.post('/driver/examine/list', this.queryExamine).then(res => {
        if (res.data.code === 200) {
          this.examineList = res.data.data.examineList
          this.total = res.data.data.total
        } else {
          this.$message.error(res.data.message)
        }
      }).catch(err => {
        console.log(err)
        this.$message.error(err.message)
      })
    },

    // 监听 size 改变的事件
    handleSizeChange(size) {
      this.queryExamine.size = size
      this.getExamineList()
    },
    // 监听 page 改变的事件
    handleCurrentChange(page) {
      this.queryExamine.page = page
      this.getExamineList()
    },
    // 结束报名
    finishAppoint(id) {
      this.$confirm('确定结束考试报名吗？结束后无法恢复', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.get('/driver/examine/finish/appoint', {
          params: {
            id: id
          }
        }).then(res => {
          if (res.data.code === 200) {
            this.$message.success(res.data.message)
            this.getExamineList()
          } else {
            this.$message.error(res.data.message)
          }
        }).catch(err => {
          console.log(err)
          this.$message.error(err.message)
        })
      })
    },
    // 安排教练
    setCoachDialog(id) {
      this.examineId = id
      // 获取考试教练
      this.$http.get('/driver/coach/getExamineCoach', {
        params: {
          id: id
        }
      }).then(res => {
        if (res.data.code === 200) {
          let data = res.data.data;
          this.examineCoach = []
          data.forEach(data => {
            this.examineCoach.push(data.id)
          })
        } else {
          this.$message.error(res.data.message)
        }
      }).catch(err => {
        console.log(err)
        this.$message.error(err.message)
      })
      // 获取所有教练
      this.$http.get('/driver/coach/getAllCoach').then(res => {
        if (res.data.code === 200) {
          let res_data = res.data.data;
          this.allCoach = []
          this.examineCoach.forEach(data => {
            res_data.filter(f => f.id !== data)
          })
          res_data.forEach(data => {
            this.allCoach.push({
              key: data.id,
              label: data.name
            })
          })
          this.setCoachDialogVisible = true;

        } else {
          this.$message.error(res.data.message)
        }
      }).catch(err => {
        console.log(err)
        this.$message.error(err.message)
      })
    },
    // 分配教练
    setCoach(){
      let coachId = []
      // console.log(this.examineCoach)
      this.examineCoach.forEach(data => {
        coachId.push(data)
      })
      let data = {
        coachId: coachId,
        examineId: this.examineId
      }
      this.$http.post('/driver/coachExamine/setCoach', data).then(res => {
        if (res.data.code === 200) {
          this.$message.success(res.data.message)
          this.setCoachDialogVisible = false
        } else {
          this.$message.error(res.data.message)
        }
      }).catch(err => {
        console.log(err)
        this.$message.error(err.message)
      })
    },
    // 报名人数设置
    examNumberHandleChange(number) {
      this.addExamineForm.examNumber = number
    },
    // 新增考试
    addExamine() {
      this.$refs.addExamineFormRef.validate(valid => {
        if (!valid) return
        console.log(this.addExamineForm)
        this.$http.post("/driver/examine/add", this.addExamineForm).then(res => {
          if (res.data.code === 200) {
            this.$message.success(res.data.message)
            this.$refs.addExamineFormRef.resetFields()
            this.addDialogVisible = false
            this.getExamineList()
          } else {
            this.$message.error(res.data.message)
          }
        }).catch(err => {
          console.log(err)
          this.$message.error(err.message)
        })
      })
    },
    // 监听新增考试对话框的关闭事件
    addDialogClosed() {
      this.$refs.addExamineFormRef.resetFields()
      this.addDialogVisible = false
    },

  }
}
</script>

<style scoped>

</style>
